package cn.community.system.web.controller;

import cn.community.system.domain.Activity;
import cn.community.system.domain.ActivityRecord;
import cn.community.system.query.ActivityRecordQuery;
import cn.community.system.service.*;
import cn.community.system.util.JsonResult;
import cn.community.system.vo.ActivityRecordVo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/activityRecord")
public class ActivityRecordController {

    @Autowired
    private IActivityRecordService activityRecordService;

    @RequestMapping("/list")
    @RequiresPermissions(value = {"activityRecord/list","超管-活动报名列表"},logical = Logical.OR)
    public String list(Model model, @ModelAttribute("qo") ActivityRecordQuery qo){
        model.addAttribute("page",activityRecordService.query(qo));
        return "activityRecord/admin/list";
    }

    @RequestMapping("/association/list")
    @RequiresPermissions(value = {"activityRecord/association/list","社团-活动报名列表"},logical = Logical.OR)
    public String listAsso(Model model, @ModelAttribute("qo") ActivityRecordQuery qo){
        model.addAttribute("page",activityRecordService.queryAsso(qo));
        return "activityRecord/association/list";
    }

    @RequestMapping("/member/list")
    @RequiresPermissions(value = {"activityRecord/member/list","社团成员-活动报名记录"},logical = Logical.OR)
    public String listMember(Model model, @ModelAttribute("qo") ActivityRecordQuery qo){
        model.addAttribute("page",activityRecordService.queryMember(qo));
        return "activityRecord/member/list";
    }

    @RequestMapping("/input")
    public String input(Model model,Long id){
        if(id!=null){ //编辑
            ActivityRecord activityRecord = activityRecordService.get(id);
            model.addAttribute("activityRecord",activityRecord);
        }
        return "activityRecord/admin/input";
    }

    @RequestMapping("/applyActivity")
    @ResponseBody
    @RequiresPermissions(value = {"activityRecord/applyActivity","活动报名"},logical = Logical.OR)
    public Object applyActivity(Long id){
        activityRecordService.applyActivity(id);
        return JsonResult.success();
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value = {"activityRecord/saveOrUpdate","超管-活动记录新增/编辑"},logical = Logical.OR)
    public Object update(ActivityRecordVo activityRecord){
        if(activityRecord.getId() != null){  // 修改
            activityRecordService.update(activityRecord);
        }else {  // 新增
            activityRecordService.save(activityRecord);
        }
        return JsonResult.success();
    }

    @RequestMapping("/saveOrUpdate2")
    @ResponseBody
    @RequiresPermissions(value = {"activityRecord/saveOrUpdate2","社团-活动记录新增/编辑"},logical = Logical.OR)
    public Object update2(ActivityRecordVo activityRecord){
        if(activityRecord.getId() != null){  // 修改
            activityRecordService.update2(activityRecord);
        }else {  // 新增
            activityRecordService.save2(activityRecord);
        }
        return JsonResult.success();
    }

    @RequestMapping("/cancelApply")
    @ResponseBody
    @RequiresPermissions(value = {"activityRecord/cancelApply","取消报名"},logical = Logical.OR)
    public JsonResult cancelApply(Long id){
        // 删除活动记录
        activityRecordService.cancelApply(id);
        return JsonResult.success();
    }

    // 报名成功
    @RequestMapping("/passOne")
    @ResponseBody
    @RequiresPermissions(value = {"activityRecord/passOne","审核-报名成功"},logical = Logical.OR)
    public JsonResult passOne(Integer hot,Long id){
        activityRecordService.passOne(id,hot);
        return JsonResult.success();
    }

    // 报名失败
    @RequestMapping("/refuseOne")
    @ResponseBody
    @RequiresPermissions(value = {"activityRecord/refuseOne","审核-报名失败"},logical = Logical.OR)
    public JsonResult refuseOne(ActivityRecord activityRecord){
        activityRecordService.refuseOne(activityRecord);
        return JsonResult.success();
    }

    // 批量审核报名成功
    @RequestMapping("/batchPass")
    @ResponseBody
    @RequiresPermissions(value = {"activityRecord/batchPass","批量审核报名成功"},logical = Logical.OR)
    public JsonResult batchPass(Long[] ids){
        activityRecordService.batchPass(ids);
        return JsonResult.success();
    }

    @RequestMapping("/delete")
    @ResponseBody
    @RequiresPermissions(value = {"activityRecord/delete","报名记录删除"},logical = Logical.OR)
    public JsonResult delete(Long id){
        activityRecordService.delete(id);
        return JsonResult.success();
    }
}
